import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    houseList: [],
    houseId:'',
    houseInfo:'',
    gender:1,
    name:'',
    mobile:'',
    visitDate:'',
    currentDate:new Date().getTime(),
    maxData:Date.now()+1000*3600*24*3
  },
  rules: {
    houseId:[ { required: true, message: '请填写房屋信息!' }],
    name:[ { required: true, message: '请填写姓名!' },
     {pattern: /[\u4e00-\u9fa5]{2,5}/, message: '访客姓名只能为5个字内的中文!'}],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请检查手机号码是否正确!' },
    ],
    visitDate: [
      { required: true, message: '请填写访问日期!' },
    ]
  },
  onLoad(){
    this.getHouseList()
  },
//获取我的楼盘信息
  async getHouseList(){
  const {code,data:houseList} = await wx.http.get('/house')
  if(code!==10000) return wx.utils.toast()
  this.setData({
    houseList
  })
},
//选择房屋触发的函数
selectId(ev){
  console.log(ev.detail);
  this.setData({
    houseId:ev.detail.id,
    houseInfo:ev.detail.name
  })
},
//选择时间触发
conDate(ev){
  this.setData({
    visitDate:wx.utils.dataFormat(ev.detail),
    dateLayerVisible: false
  })
},

  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  async goPassport() {
    if(!this.validate()) return
    const {houseId,name,gender,mobile,visitDate} = this.data
    const {code,data:{id}} = await wx.http.post('/visitor',{
      houseId,name,gender,mobile,visitDate
    })
    if(code!==10000) return wx.utils.toast()
    // console.log(id);
    wx.reLaunch({
      url: '/visitor_pkg/pages/passport/index?id='+id,
    })
  },
})
